package com.demo.user.response;

import com.google.common.collect.Lists;
import com.demo.user.exception.DisplayableError;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.exception.ExceptionUtils;

import static com.demo.user.message.MessageConstant.MESSAGE_FAIL;
import static com.demo.user.message.MessageConstant.MESSAGE_SUCCESS;

public final class ResponseFactory {

    private static final String DEFAULT_CODE = "200";
    private static final String DEFAULT_CODE_ERROR = "500";

    private ResponseFactory() {
    }

    public static Response toSuccess() {
        return toSuccess(MESSAGE_SUCCESS, null);
    }

//    public static <T> Response<T> toSuccess(String msg) {
//        return toSuccess(msg , null);
//    }

    public static <T> Response<T> toSuccessStringData(String data) {
        return (Response<T>) toSuccess(MESSAGE_SUCCESS , data);
    }

    public static <T> Response<T> toSuccess(T data) {
        return new Response<>(true, DEFAULT_CODE, MESSAGE_SUCCESS, data);
    }

    public static <T> Response<T> toSuccessList(T data) {
        if (data == null) {
            return new Response(true, DEFAULT_CODE, MESSAGE_SUCCESS, Lists.newArrayList());
        }
        return new Response<>(true, DEFAULT_CODE, MESSAGE_SUCCESS, data);
    }

    public static <T> Response<T> toOpenApiSuccess(String msg, T data) {
        return new Response<>(true, "SUCCESS", msg, data);
    }

    public static <T> Response<T> toBizError(String msg, String code, T data) {
        return new Response<>(true, code, msg, data);
    }

    public static <T> Response<T> toOpenApiBizError(String msg, T data) {
        return new Response<>(true, "BUSINESS_ERROR", msg, data);
    }

    public static <T> Response<T> toSuccess(String msg, T data) {
        return new Response<>(true, DEFAULT_CODE, msg, data);
    }

    public static Response<String> toWarn(DisplayableError warnCode) {
        return new Response<>(true, warnCode.getErrorCode(), warnCode.getDisplayMsg(),
            MESSAGE_SUCCESS);
    }
    public static <T> Response<T> toError() {
        return new Response<>(false, DEFAULT_CODE_ERROR, MESSAGE_FAIL, null);
    }

    public static <T> Response<T> toError(String msg) {
        return new Response<>(false, DEFAULT_CODE_ERROR, msg, null);
    }

    public static <T> Response<T> toError(String code, String msg) {
        return toError(code, msg, null);
    }

    public static <T> Response<T> toError(String code, String msg, T data) {
        return new Response<>(false, code, msg, data);
    }

    public static Response<String> toErrorWithException(Throwable e, boolean isProd) {
        return toErrorWithException(null, null, e, isProd);
    }

    public static Response<String> toErrorWithException(String code, String msg, Throwable e, boolean isProd) {
        if (StringUtils.isBlank(code)) {
            code = DEFAULT_CODE_ERROR;
        }

        Response<String> response = new Response<>();
        response.setSuccess(false);
        response.setMessageCode(code);
        response.setMessage(msg);

        if (!isProd) {
            response.setData(ExceptionUtils.getStackTrace(e));
        }
        return response;
    }
}
